package com.krain.ddbb.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.krain.corelibrary.helper.imageloader.ImageLoader;
import com.krain.corelibrary.util.UIUtil;
import com.krain.ddbb.R;
import com.krain.ddbb.base.BaseActivity;
import com.krain.ddbb.constant.Constant;
import com.krain.ddbb.entity.AppApi;
import com.krain.ddbb.entity.BaseEntity;
import com.krain.ddbb.entity.RobOrderBean;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * Created by huyamin on 2016/3/9.
 */
@EActivity(R.layout.activity_order_detail)
public class RobOrderActivity extends BaseActivity {
    @ViewById(R.id.act_order_detail_desc)
    TextView tv_desc;

    @ViewById(R.id.act_order_detail_phone)
    TextView tv_phone;

    @ViewById(R.id.act_order_detail_adress)
    TextView tv_adress;

    @ViewById(R.id.act_order_detail_contact)
    TextView tv_contact;

    @ViewById(R.id.act_order_detail_num)
    TextView tv_num;

    @ViewById(R.id.act_order_detail_price)
    TextView tv_price;

    @ViewById(R.id.act_order_detail_servetime)
    TextView tv_serveTime;

    @ViewById(R.id.act_order_detail_time)
    TextView tv_time;

    @ViewById(R.id.act_again_order)
    Button btnRob;//抢单
    @Extra("oid")
    String order_id;
    RobOrderBean bean;
    @ViewById(R.id.ll_pics)
    LinearLayout ll_pics;

    @Override
    protected String getToolbarTitle() {
        return "抢单" ;
    }

    @AfterViews
    void afterViews(){
        findViewById(R.id.id_rel_robmsg).setVisibility(View.GONE);
        btnRob.setText("抢单");
        btnRob.setVisibility(View.VISIBLE);
        getRobmsg();
        tv_phone.setVisibility(View.GONE);
    }


    @Click(R.id.act_again_order)
    void submitRob(){
        submitAtBackground();
    }

    @Background
    void getRobmsg(){
        showProgressDialog();
        RobOrderBean entity = AppApi.getsInstance(mContext).getRobOrderMsg(app.getmUserinfo().getAccess_token(), order_id);
        if (entity!=null)
        setRobmsg(entity);
        dismissProgressDialog();
    }

    @UiThread
    void setRobmsg(RobOrderBean bean){
        this.bean = bean;
        tv_desc.setText(getString(R.string.order_need,bean.getDesc()));
        tv_adress.setText(getString(R.string.serve_address,bean.getServer_address()));
        tv_num.setText(getString(R.string.order_number,bean.getOrder_num()));
        tv_price.setText(getString(R.string.serve_price,bean.getServer_price()));
        tv_serveTime.setText(getString(R.string.serve_time,bean.getServer_time()));
        tv_contact.setVisibility(View.GONE);
        tv_time.setVisibility(View.GONE);
        if (bean.getImage() != null && bean.getImage().size()>0){
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150,150);
            params.setMargins(10,10,10,10);
            for (final String str:bean.getImage()) {
                ImageView imageView = new ImageView(this);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                ImageLoader.getInstance(mContext).loadImage(mContext,imageView, Constant.HOSTURL+str,R.mipmap.ic_launcher,200,200);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext,PhotoViewActivity.class);
                        intent.putExtra("url",Constant.HOSTURL+str);
                        startActivity(intent);
                    }
                });
                ll_pics.addView(imageView,params);

            }
        }




    }

    @Background
    void submitAtBackground(){
        showProgressDialog();
      BaseEntity entity = AppApi.getsInstance(mContext).robOrdeer(app.getmUserinfo().getAccess_token(),String.valueOf(bean.getOrder_id()));
        if (entity != null) {
            baseUtil.showSnackBar(mContext,"恭喜抢单成功","点击去查看详情",toDetailListener);
        }else {
            baseUtil.showSnackBar(mContext,"抢单失败,您的单子被别人抢走啦！","关闭页面",onClickListener);
        }
        dismissProgressDialog();
        }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    };

    View.OnClickListener toDetailListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            UserOrderActivity.jumpTothisActivity(mContext);
        }
    };

}
